package com.interview.orderdetail.repository;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

import org.springframework.stereotype.Repository;

import com.interview.contacts.entity.Contacts;
import com.interview.core.page.Page;
import com.interview.orderdetail.entity.OrderDetail;
import com.interview.orderdetail.query.OrderlistConditionQuery;

@Repository
public class OrderlistRepository {

	@PersistenceContext
	private EntityManager entityManager;

	public OrderDetail findOneOrderList(String id) {
		return entityManager.find(OrderDetail.class, id);
	}

	public OrderDetail saveOrderList(OrderDetail orderlist) {
		entityManager.persist(orderlist);
		return orderlist;
	}

	public OrderDetail updateOrderList(OrderDetail orderList) {
		entityManager.merge(orderList);
		return orderList;
	}

	public void deleteOrderList(String id) {
		entityManager.remove(findOneOrderList(id));
	}

	public List<OrderDetail> findAllOrderList() {
		CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
		CriteriaQuery<OrderDetail> criteriaQuery = criteriaBuilder
				.createQuery(OrderDetail.class);
		Root<OrderDetail> root = criteriaQuery.from(OrderDetail.class);

		List<OrderDetail> orderList = entityManager.createQuery(criteriaQuery)
				.getResultList();

		return orderList;
	}

	public List<OrderDetail> findOrderListByOrderlistConditionQuery(

	OrderlistConditionQuery conditionQuery) {

		String hqlHead = "select ord from SalesOrder sa join sa.orderList  ord  where sa.id = :id ";
		StringBuilder hql = new StringBuilder();

		String salesOrderId = conditionQuery.getSalesOrderId();
		String productName = conditionQuery.getProductName();

		Double discount = conditionQuery.getDiscount();
		Double productCostBegin = conditionQuery.getProductCostBegin();
		Double productCostEnd = conditionQuery.getProductCostEnd();
		Integer productNumBegin = conditionQuery.getProductNumBegin();
		Integer productNumEnd = conditionQuery.getProductNumEnd();

		hql.append(hqlHead);

		if (productName == "" & productName.contains(productName)) {
			productName = null;
		}

		if (productName != null) {
			productName = "%" + productName + "%";

			String hqlProductName = " and ord.productName like :productName ";
			hql.append(productName);
		}

		if (discount != null) {
			String hqlDiscount = " and ord.discount = :discount ";
			hql.append(hqlDiscount);
		}

		if (productCostBegin != null) {

			String hqlProductCostBegin = " and ord.productCost >= :productCostBegin";
			hql.append(hqlProductCostBegin);

		}

		if (productCostEnd != null) {
			String hqlProductCostEnd = " and ord.productCost <= :productCostEnd ";
			hql.append(hqlProductCostEnd);
		}

		if (productNumBegin != null) {
			String hqlProductNumBegin = " and ord.productNum >= :productNumBegin ";
			hql.append(hqlProductNumBegin);
		}

		if (productNumEnd != null) {

			String hqlProductNumEnd = " and ord.productNum <= :productNumEnd ";
			hql.append(hqlProductNumEnd);
		}

		Query query = entityManager.createQuery(hql.toString());
		query.setParameter("id", salesOrderId);

		if (productCostBegin != null) {
			query.setParameter("productCostBegin", productCostBegin);
		}
		if (productCostEnd != null) {
			query.setParameter("productCostEnd", productCostEnd);
		}
		if (productNumBegin != null) {
			query.setParameter("productNumBegin", productNumBegin);
		}

		if (productNumEnd != null) {
			query.setParameter("productNumEnd", productNumEnd);
		}

		if (productName != null) {
			query.setParameter("productName", productName);
		}

		if (discount != null) {
			query.setParameter("discount", discount);
		}

		List<OrderDetail> orderLists = query.getResultList();
		
		return orderLists;
	}
}
